package org.fyp.synchrona.blog.service;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.fyp.synchrona.core.info.impl.ICommonDataObject;

import org.fyp.synchrona.service.IService;

import org.fyp.synchrona.util.IAccount;
import org.fyp.synchrona.util.IUpdate;

public class BloggerService implements IService {

	private String serviceName = "Blogger";

	public String userName;

	public String password;

	static final Logger logger = Logger.getLogger(BloggerService.class);

	public void executeTask() {

	}

	public void startService(IUpdate updateObj) {
		logger.info("[BLOGGER] ######################Start Information from Blogger Service########################");
		getuserNamePassword(updateObj.getAccount());
		logger.info("[BLOGGER] userName :: " + userName);
		logger.info("[BLOGGER] password :: " + password);
		Updater updater = new Updater(userName, password);
		updater.startUpdate(updateObj);
		logger.info("[BLOGGER] ######################End Information from Blogger Service#########################");
	}

	private void getuserNamePassword(List<IAccount> account) {

		IAccount element = null;
		Iterator i = account.listIterator();

		logger.info("[BLOGGER] +_+_+_+_+_+_+_+_+_+_+_+_+_+getting Username and Password_+_+_+_+_+_+_+_+_+_+_+_+_+_+");
		while (i.hasNext()) {

			element = (IAccount) i.next();
			logger.info("[BLOGGER] ++++++++++++++++++++++++service name+++++++++++++++++++++++++++");

			logger.info("[BLOGGER] serviceName ::"+element.getServiceName() );
			if (element.getServiceName().equals(serviceName)) {
				this.userName = element.getUserName();
				this.password = element.getPassword();
				break;
			}
			logger.info("[BLOGGER] ++++++++++++++++++++++++user name+++++++++++++++++++++++++++");
			logger.info("[BLOGGER] username ::"+element.getUserName() );
			logger.info("[BLOGGER] ++++++++++++++++++++++++password+++++++++++++++++++++++++++");
			logger.info("[BLOGGER] password ::"+element.getUserName() );
			
		}

	}

	public static void main(String arg[]) {
		ICommonDataObject cpo = (ICommonDataObject) new CommonDataObject();
		cpo.setBIO("test2");
		System.out.println(cpo.getBIO());
		cpo.setBIRTH_DAY("2");
		cpo.setBIRTH_MONTH("2");
		cpo.setBIRTH_YEAR("1990");
		IUpdate updateObj = new Update();
		updateObj.setUpdateType("PROFILE");
		updateObj.setCdo(cpo);

		new BloggerService().startService(updateObj);
	}

}
